﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FACUI.BasicClass
{


    public class GetKglInfo
    {
        private int kglmaxID = 0;
        private CfgNode ljkglNode = null;
        private GetMapItem mapitem = null;
        private CfgNode sjKglNode = null;

        public GetKglInfo(StationCfg staCfg)
        {
            if (staCfg.GetKglCfgNode() != null)
            {
                this.sjKglNode = staCfg.GetKglCfgNode().GetSubNode("实际开关量", true);
                this.ljkglNode = staCfg.GetKglCfgNode().GetSubNode("智能分析逻辑开关量");
                this.mapitem = new GetMapItem(staCfg);
                this.GetKglADList();
            }
        }

        public Dictionary<string, Kgl> GetFixKglList(string sufFix)
        {
            Kgl kgl;
            string str2;
            Dictionary<string, Kgl> dictionary = new Dictionary<string, Kgl>();
            foreach (CfgNode node in this.sjKglNode.GetSubNodeList())
            {
                string str = node["开关量类型"];
                str2 = node["开关量名称"];
                if (str2.EndsWith(sufFix))
                {
                    kgl = new Kgl(str2)
                    {
                        SubType = str,
                        AdNo = Util.ToInt(node["AD号"], -1),
                        FjNo = Util.ToInt(node["分机号"], -1),
                        FjAd = Util.ToInt(node["分机内AD号"], -1),
                        id = Util.ToInt(node["ID"], -1)
                    };
                    if (!dictionary.ContainsKey(str2))
                    {
                        dictionary.Add(str2, kgl);
                    }
                }
            }
            if (this.ljkglNode != null)
            {
                foreach (CfgNode node in this.ljkglNode.GetSubNodeList())
                {
                    str2 = node["开关量名称"];
                    if (str2.EndsWith(sufFix))
                    {
                        kgl = new Kgl(str2)
                        {
                            SubKgl1Name = node["子开关量1"],
                            SubKgl2Name = node["子开关量2"],
                            SubKglOp = node["逻辑关系"],
                            Note = node["备注"],
                            id = Util.ToInt(node["ID"], -1)
                        };
                        if (!dictionary.ContainsKey(str2))
                        {
                            dictionary.Add(str2, kgl);
                        }
                    }
                }
            }
            return dictionary;
        }

        public Dictionary<string, string> GetKglADList()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            int num = 0;
            if (this.sjKglNode != null)
            {
                foreach (CfgNode node in this.sjKglNode.GetSubNodeList())
                {
                    num = Util.ToInt(node["ID"], -1);
                    this.kglmaxID = (this.kglmaxID < num) ? num : this.kglmaxID;
                    if (!dictionary.ContainsKey(node["开关量名称"]))
                    {
                        dictionary.Add(node["开关量名称"], node["AD号"]);
                    }
                }
                if (this.ljkglNode != null)
                {
                    foreach (CfgNode node in this.ljkglNode.GetSubNodeList())
                    {
                        num = Util.ToInt(node["ID"], -1);
                        this.kglmaxID = (this.kglmaxID < num) ? num : this.kglmaxID;
                        if (!dictionary.ContainsKey(node["开关量名称"]))
                        {
                            dictionary.Add(node["开关量名称"], node["AD号"]);
                        }
                    }
                }
            }
            return dictionary;
        }

        public List<Kgl> GetKglList(string kglType)
        {
            List<Kgl> list = new List<Kgl>();
            foreach (CfgNode node in this.sjKglNode.GetSubNodeList())
            {
                string str = node["开关量类型"];
                string kglName = node["开关量名称"];
                if (str == kglType)
                {
                    Kgl item = new Kgl(kglName)
                    {
                        SubType = str,
                        AdNo = Util.ToInt(node["AD号"], -1),
                        FjNo = Util.ToInt(node["分机号"], -1),
                        FjAd = Util.ToInt(node["分机内AD号"], -1),
                        id = Util.ToInt(node["ID"], -1),
                        Note = node["备注"]
                    };
                    list.Add(item);
                }
            }
            return list;
        }

        public Dictionary<string, Kgl> GetKglList(List<string> devNames, string sufFix)
        {
            Dictionary<string, Kgl> dictionary = new Dictionary<string, Kgl>();
            Dictionary<string, Kgl> fixKglList = this.GetFixKglList(sufFix);
            foreach (string str in devNames)
            {
                if (fixKglList.ContainsKey(str + sufFix))
                {
                    if (!dictionary.ContainsKey(str))
                    {
                        dictionary.Add(str, fixKglList[str + sufFix]);
                    }
                }
                else if (!dictionary.ContainsKey(str))
                {
                    dictionary.Add(str, new Kgl(""));
                }
            }
            return dictionary;
        }

        public int GetKglMaxID()
        {
            return this.kglmaxID;
        }

        public List<string> GetKglNameList(string kglType, string sufFix)
        {
            List<string> list = new List<string>();
            foreach (CfgNode node in this.sjKglNode.GetSubNodeList())
            {
                string str = node["分机号"];
                string str2 = node["开关量类型"];
                string item = node["开关量名称"].ToString();
                if (((!list.Contains(item) && (str2 == kglType)) && !string.IsNullOrEmpty(sufFix)) && item.EndsWith(sufFix))
                {
                    item = item.ToUpper().Replace(sufFix, "");
                    list.Add(item);
                }
            }
            return list;
        }

        private void GetMapKgl(Dictionary<string, string> kgllist)
        {
            Dictionary<string, MapLAnItem> mapLANItems = this.mapitem.GetMapLANItems(-1);
            foreach (MapLAnItem item in mapLANItems.Values)
            {
                if (!string.IsNullOrEmpty(item.SignalFlag) && !kgllist.ContainsKey(item.SignalFlag))
                {
                    kgllist.Add(item.SignalFlag, item.SignalFlagAD);
                }
                if (!string.IsNullOrEmpty(item.Red) && !kgllist.ContainsKey(item.Red))
                {
                    kgllist.Add(item.Red, item.RedAD);
                }
                if (!string.IsNullOrEmpty(item.Blue) && !kgllist.ContainsKey(item.Blue))
                {
                    kgllist.Add(item.Blue, item.BlueAD);
                }
                if (!string.IsNullOrEmpty(item.White) && !kgllist.ContainsKey(item.White))
                {
                    kgllist.Add(item.White, item.WhiteAD);
                }
                if (!string.IsNullOrEmpty(item.Green) && !kgllist.ContainsKey(item.Green))
                {
                    kgllist.Add(item.Green, item.GreenAD);
                }
                if (!string.IsNullOrEmpty(item.Yellow) && !kgllist.ContainsKey(item.Yellow))
                {
                    kgllist.Add(item.Yellow, item.YellowAD);
                }
            }
            Dictionary<string, MapXhjItem> mapXHJItems = this.mapitem.GetMapXHJItems(-1);
            foreach (MapXhjItem item2 in mapXHJItems.Values)
            {
                if (!string.IsNullOrEmpty(item2.Red) && !kgllist.ContainsKey(item2.Red))
                {
                    kgllist.Add(item2.Red, item2.RedAD);
                }
                if (!string.IsNullOrEmpty(item2.Green) && !kgllist.ContainsKey(item2.Green))
                {
                    kgllist.Add(item2.Green, item2.GreenAD);
                }
                if (!string.IsNullOrEmpty(item2.Yellow) && !kgllist.ContainsKey(item2.Yellow))
                {
                    kgllist.Add(item2.Yellow, item2.YellowAD);
                }
                if (!string.IsNullOrEmpty(item2.White) && !kgllist.ContainsKey(item2.White))
                {
                    kgllist.Add(item2.White, item2.WhiteAD);
                }
                if (!string.IsNullOrEmpty(item2.Blue) && !kgllist.ContainsKey(item2.Blue))
                {
                    kgllist.Add(item2.Blue, item2.BlueAD);
                }
                if (!string.IsNullOrEmpty(item2.DGreen) && !kgllist.ContainsKey(item2.DGreen))
                {
                    kgllist.Add(item2.DGreen, item2.DGreenAD);
                }
                if (!string.IsNullOrEmpty(item2.DYellow) && !kgllist.ContainsKey(item2.DYellow))
                {
                    kgllist.Add(item2.DYellow, item2.DYellowAD);
                }
                if (!string.IsNullOrEmpty(item2.YellowFlashYellow) && !kgllist.ContainsKey(item2.YellowFlashYellow))
                {
                    kgllist.Add(item2.YellowFlashYellow, item2.YellowFlashYellowAD);
                }
                if (!string.IsNullOrEmpty(item2.RedFlash) && !kgllist.ContainsKey(item2.RedFlash))
                {
                    kgllist.Add(item2.RedFlash, item2.RedFlashAD);
                }
                if (!string.IsNullOrEmpty(item2.WhiteFlash) && !kgllist.ContainsKey(item2.WhiteFlash))
                {
                    kgllist.Add(item2.WhiteFlash, item2.WhiteFlashAD);
                }
                if (!string.IsNullOrEmpty(item2.GreenFlash) && !kgllist.ContainsKey(item2.GreenFlash))
                {
                    kgllist.Add(item2.GreenFlash, item2.GreenFlashAD);
                }
                if (!string.IsNullOrEmpty(item2.YellowFlash) && !kgllist.ContainsKey(item2.YellowFlash))
                {
                    kgllist.Add(item2.YellowFlash, item2.YellowFlashAD);
                }
                if (!string.IsNullOrEmpty(item2.GreenYellow) && !kgllist.ContainsKey(item2.GreenYellow))
                {
                    kgllist.Add(item2.GreenYellow, item2.GreenYellowAD);
                }
                if (!string.IsNullOrEmpty(item2.RedWhite) && !kgllist.ContainsKey(item2.RedWhite))
                {
                    kgllist.Add(item2.RedWhite, item2.RedWhiteAD);
                }
            }
            Dictionary<string, MapDcItem> mapDcItems = this.mapitem.GetMapDcItems(-1);
            foreach (MapDcItem item3 in mapDcItems.Values)
            {
                if (!string.IsNullOrEmpty(item3.Dkgl) && !kgllist.ContainsKey(item3.Dkgl))
                {
                    kgllist.Add(item3.Dkgl, item3.DkglAD);
                }
                if (!string.IsNullOrEmpty(item3.Fkgl) && !kgllist.ContainsKey(item3.Fkgl))
                {
                    kgllist.Add(item3.Fkgl, item3.FkglAD);
                }
                if (!string.IsNullOrEmpty(item3.qRed) && !kgllist.ContainsKey(item3.qRed))
                {
                    kgllist.Add(item3.qRed, item3.qRedAD);
                }
                if (!string.IsNullOrEmpty(item3.qWhite) && !kgllist.ContainsKey(item3.qWhite))
                {
                    kgllist.Add(item3.qWhite, item3.qWhiteAD);
                }
                if (!string.IsNullOrEmpty(item3.dRed) && !kgllist.ContainsKey(item3.dRed))
                {
                    kgllist.Add(item3.dRed, item3.dRedAD);
                }
                if (!string.IsNullOrEmpty(item3.dWhite) && !kgllist.ContainsKey(item3.dWhite))
                {
                    kgllist.Add(item3.dWhite, item3.dWhiteAD);
                }
                if (!string.IsNullOrEmpty(item3.fRed) && !kgllist.ContainsKey(item3.fRed))
                {
                    kgllist.Add(item3.fRed, item3.fRedAD);
                }
                if (!string.IsNullOrEmpty(item3.fWhite) && !kgllist.ContainsKey(item3.fWhite))
                {
                    kgllist.Add(item3.fWhite, item3.fWhiteAD);
                }
            }
            Dictionary<string, MapWcqdItem> mapWcqdItems = this.mapitem.GetMapWcqdItems(-1);
            foreach (MapWcqdItem item4 in mapWcqdItems.Values)
            {
                if (!string.IsNullOrEmpty(item4.gdRed) && !kgllist.ContainsKey(item4.gdRed))
                {
                    kgllist.Add(item4.gdRed, item4.gdRedAD);
                }
                if (!string.IsNullOrEmpty(item4.gdWhite) && !kgllist.ContainsKey(item4.gdWhite))
                {
                    kgllist.Add(item4.gdWhite, item4.gdWhiteAD);
                }
            }
        }
    }
}

